/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.web.arrivalreminder;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.warehourseEnum;
import com.jeesite.modules.financial.entity.arrivalreminder.ArrivalReminder;
import com.jeesite.modules.financial.entity.lettercredit.LetterCredit;
import com.jeesite.modules.financial.service.arrivalreminder.ArrivalReminderService;
import com.jeesite.modules.financial.service.lettercredit.LetterCreditService;
import com.jeesite.modules.logistics.entity.ladingbill.LadingBill;
import com.jeesite.modules.logistics.service.ladingbill.LadingBillService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 到单提醒Controller
 *
 * @author LV
 * @version 2021-03-04
 */
@Controller
@RequestMapping(value = "${adminPath}/financial/arrivalreminder/arrivalReminder")
public class ArrivalReminderController extends BaseController {

    @Autowired
    private ArrivalReminderService arrivalReminderService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private LadingBillService ladingBillService;
    @Autowired
    private LetterCreditService letterCreditService;


    /**
     * 获取数据
     */
    @ModelAttribute
    public ArrivalReminder get(String arrivalReminderId, boolean isNewRecord) {
        return arrivalReminderService.get(arrivalReminderId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:view")
    @RequestMapping(value = {"list", ""})
    public String list(ArrivalReminder arrivalReminder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listLading", ladingBillService.findList(new LadingBill()));// 提单号
        model.addAttribute("listLetter", letterCreditService.findList(new LetterCredit()));// 信用证
        model.addAttribute("listCustomer", customerService.findList(new Customer()));

        Supplier supplierIN = new Supplier();
        supplierIN.setSupplierType(warehourseEnum.GUONEIZIFANG.getCode()); // 国内资方
        model.addAttribute("listSupplierIN", supplierService.findList(supplierIN));

        Supplier supplierFOR = new Supplier();
        supplierFOR.setSupplierType(warehourseEnum.HAIWAIZIFANG.getCode()); // 海外资方
        model.addAttribute("listSupplierFOR", supplierService.findList(supplierFOR));

        Supplier supplier = new Supplier();
        supplier.setSupplierType(warehourseEnum.GONGHUOWAISHANG.getCode()); // 供货外商
        model.addAttribute("listSupplier", supplierService.findList(supplier));

        model.addAttribute("listUser", empUserService.findList(new EmpUser()));
        model.addAttribute("arrivalReminder", arrivalReminder);
        return "modules/financial/arrivalreminder/arrivalReminderList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page < ArrivalReminder > listData(ArrivalReminder arrivalReminder, HttpServletRequest request, HttpServletResponse response) {
        arrivalReminder.setPage(new Page <>(request, response));
        Page < ArrivalReminder > page = arrivalReminderService.findPage(arrivalReminder);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:view")
    @RequestMapping(value = "form")
    public String form(ArrivalReminder arrivalReminder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listLading", ladingBillService.findList(new LadingBill()));// 提单号
        model.addAttribute("listLetter", letterCreditService.findList(new LetterCredit()));// 信用证
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("listSupplier", supplierService.findList(new Supplier()));
        model.addAttribute("arrivalReminder", arrivalReminder);
        return "modules/financial/arrivalreminder/arrivalReminderForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:view")
    @RequestMapping(value = "detail")
    public String detail(ArrivalReminder arrivalReminder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listLading", ladingBillService.findList(new LadingBill()));// 提单号
        model.addAttribute("listLetter", letterCreditService.findList(new LetterCredit()));// 信用证
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("listSupplier", supplierService.findList(new Supplier()));
        model.addAttribute("arrivalReminder", arrivalReminder);
        return "modules/financial/arrivalreminder/arrivalReminderDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated ArrivalReminder arrivalReminder) {
        arrivalReminderService.save(arrivalReminder);
        return renderResult(Global.TRUE, text("保存到单提醒成功！"));
    }

    /**
     * 删除数据收支明细单
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(ArrivalReminder arrivalReminder) {
        arrivalReminderService.delete(arrivalReminder);
        return renderResult(Global.TRUE, text("删除到单提醒成功！"));
    }

    /**
     * 列表选择对话框
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:view")
    @RequestMapping(value = "arrivalReminderSelect")
    public String empUserSelect(ArrivalReminder arrivalReminder, String selectData, Model model) {
        String selectDataJson = EncodeUtils.decodeUrl(selectData);
        if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()) {
            model.addAttribute("selectData", selectDataJson);
        }
        model.addAttribute("ArrivalReminder", arrivalReminder);
        return "modules/financial/arrivalreminder/arrivalReminderSelect";
    }

    /**
     * 单据流传创建-创建自服务订单
     */
    @RequiresPermissions("financial:arrivalreminder:arrivalReminder:edit")
    @RequestMapping(value = "createArrivalReminder")
    public String createArrivalReminder(ServiceOrder serviceOrder, Model model) {
        ServiceOrder serviceOrder1 = serviceOrderService.get(serviceOrder);
        if (serviceOrder1 != null) {
            ArrivalReminder arrivalReminder = new ArrivalReminder();
            // 服务订单
            arrivalReminder.setServiceOrderId(serviceOrder1.getServiceOrderId());
            // 客户
            arrivalReminder.setCustomerId(serviceOrder1.getCustomerId());
            //国内资方
            arrivalReminder.setInvestorId(serviceOrder1.getInvestorId());
            // 国外资方
            arrivalReminder.setForeignBusinessmenId(serviceOrder1.getForeignBusinessmenId());
            // 供货外商
            arrivalReminder.setSupplierId(serviceOrder1.getSupplierId());
            // 提单
            arrivalReminder.setLetterCreditCode(serviceOrder1.getLetterCreditCode());
            // 信用证
            arrivalReminder.setForeignTradeContract(serviceOrder1.getForeignTradeContractNumber());
            // 未到单数量
            List < ServiceOrderDetail > serviceOrderDetailList = serviceOrder1.getServiceOrderDetailList();
            // 服务订单没有车架号的
            /*for (ServiceOrderDetail serviceOrderDetail : serviceOrderDetailList) {
                if (StringUtils.isEmpty(serviceOrderDetail.getFrameNumber())){
                    allQty = allQty + serviceOrderDetail.getQuantity();
                }
            }*/
            // 服务订单车的数量-提单的车的数量
            Integer soListCount = serviceOrderDetailList.size();
            Integer ladListCount = ladingBillService.findListDetail(serviceOrder.getServiceOrderId());
            Long allQty = soListCount.longValue() - ladListCount.longValue();
            arrivalReminder.setNotArrivedQty(allQty);

            model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
            model.addAttribute("listLading", ladingBillService.findList(new LadingBill()));// 提单号
            model.addAttribute("listLetter", letterCreditService.findList(new LetterCredit()));// 信用证
            model.addAttribute("listCustomer", customerService.findList(new Customer()));
            model.addAttribute("listSupplier", supplierService.findList(new Supplier()));
            model.addAttribute("arrivalReminder", arrivalReminder);
            return "modules/financial/arrivalreminder/arrivalReminderForm";
        } else {
            return renderResult(Global.FALSE, text("未查找到服务订单的相关信息！"));
        }

    }


}